using Circus.CodeEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
namespace Circus.Syntax
{
	public class StringList : List<string>, IStringList
	{
		private string lineTerminator = "\r\n";
		public new virtual string this[int index]
		{
			get
			{
				return base[index];
			}
			set
			{
				base[index] = value;
			}
		}
		[Description("Gets or sets the strings in the \"SyntaxStrings\" as a single string with the individual strings delimited by carriage returns.")]
		public virtual string Text
		{
			get
			{
				StringBuilder stringBuilder = new StringBuilder();
				foreach (string current in this)
				{
					stringBuilder.Append(current + this.lineTerminator);
				}
				if (stringBuilder.Length >= 2)
				{
					stringBuilder.Remove(stringBuilder.Length - 2, 2);
				}
				return stringBuilder.ToString();
			}
			set
			{
				base.Clear();
				if (value != null && value != string.Empty)
				{
					StringReader stringReader = new StringReader(value);
					string item;
					while ((item = stringReader.ReadLine()) != null)
					{
						base.Add(item);
					}
				}
				this.OnTextChanged();
			}
		}
		[DefaultValue("\r\n"), Description("Gets or sets a string value that terminates line.")]
		public string LineTerminator
		{
			get
			{
				return this.lineTerminator;
			}
			set
			{
				if (this.lineTerminator != value)
				{
					this.lineTerminator = value;
					this.OnLineTerminatorChanged();
				}
			}
		}
		protected virtual void OnTextChanged()
		{
		}
		protected virtual void OnLineTerminatorChanged()
		{
		}
		public StringList()
		{
		}
		public StringList(TextReader reader)
		{
			string item;
			while ((item = reader.ReadLine()) != null)
			{
				base.Add(item);
			}
		}
		public virtual bool LoadFile(string fileName)
		{
			return this.LoadFile(fileName, null);
		}
		public virtual bool LoadFile(string fileName, Encoding encoding)
		{
			bool result = true;
			try
			{
				Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
				try
				{
					StreamReader streamReader = (encoding != null) ? new StreamReader(stream, encoding) : new StreamReader(stream);
					try
					{
						this.LoadStream(streamReader);
					}
					finally
					{
						streamReader.Close();
					}
				}
				finally
				{
					stream.Close();
				}
			}
			catch (Exception exception)
			{
				ErrorHandler.Error(exception);
				result = false;
			}
			return result;
		}
		public virtual bool LoadStream(TextReader reader)
		{
			base.Clear();
			string item;
			while ((item = reader.ReadLine()) != null)
			{
				base.Add(item);
			}
			return true;
		}
		public virtual bool LoadStream(Stream stream)
		{
			return this.LoadStream(stream, null);
		}
		public virtual bool LoadStream(Stream stream, Encoding encoding)
		{
			TextReader textReader = (encoding != null) ? new StreamReader(stream, encoding) : new StreamReader(stream);
			bool result;
			try
			{
				result = this.LoadStream(textReader);
			}
			finally
			{
				textReader.Close();
			}
			return result;
		}
		public virtual bool SaveFile(string fileName)
		{
			return this.SaveFile(fileName, null);
		}
		public virtual bool SaveFile(string fileName, Encoding encoding)
		{
			bool result = true;
			try
			{
				Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
				try
				{
					StreamWriter streamWriter = (encoding != null) ? new StreamWriter(stream, encoding) : new StreamWriter(stream);
					try
					{
						this.SaveStream(streamWriter);
					}
					finally
					{
						streamWriter.Close();
					}
				}
				finally
				{
					stream.Close();
				}
			}
			catch (Exception exception)
			{
				ErrorHandler.Error(exception);
				result = false;
			}
			return result;
		}
		public virtual bool SaveStream(Stream stream)
		{
			return this.SaveStream(stream, null);
		}
		public virtual bool SaveStream(Stream stream, Encoding encoding)
		{
			TextWriter textWriter = (encoding != null) ? new StreamWriter(stream, encoding) : new StreamWriter(stream);
			bool result;
			try
			{
				result = this.SaveStream(textWriter);
			}
			finally
			{
				textWriter.Close();
			}
			return result;
		}
		public virtual bool SaveStream(TextWriter writer)
		{
			foreach (string current in this)
			{
				writer.WriteLine(current);
			}
			return true;
		}
	}
}
